package com.qf.bigdata.repository;

import com.qf.bigdata.util.Sample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
//@Component
public class PrestoQuery {

    @Autowired //默认是按照byType注入
//    @Qualifier("prestoJdbcTemplate")
    private JdbcTemplate prestoJdbcTemplate;

    public List<Sample> queryRetention(String startDate, String endDate, int gap){
        String sql="select \n" +
                "gap , \n" +
                "sum(retention_num)*1.000/sum(new_num) as rr  \n" +
                "from dwb_news.rsu \n" +
                "where biz_date between ? and ? and gap between 1 and ? \n" +
                "group by gap \n" +
                "order by gap";
        SqlRowSet sqlRowSet = prestoJdbcTemplate.queryForRowSet(sql, startDate, endDate, gap);
        List<Sample> samples=new ArrayList<>();
        while(sqlRowSet.next()){
            samples.add(new Sample(sqlRowSet.getDouble("gap"),sqlRowSet.getDouble("rr")));
        }
        return samples;
    }
}
